//
//  CollectedPaperListController.m
//  DoingPaper_LS
//
//  Created by zhuxuhong on 2016/10/25.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//


#import "CollectedPaperListController.h"
#import "PaperCollect.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import "DoingPaperMainController.h"


#pragma mark - navigation controller

@implementation CollectedPaperController

-(instancetype)initWithPaperParam:(NSDictionary *)dict{
    CollectedPaperListController *listVC = [[CollectedPaperListController alloc] initWithPaperParam:[PaperParam paramWithDictionary:dict]];
    return [super initWithRootViewController:listVC];
}

@end




#pragma mark - root view controller

@interface CollectedPaperCell : UITableViewCell
@end

@interface CollectedPaperCell ()

@property(nonatomic,strong)UIImageView *iconView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *infoLabel;
@property(nonatomic,strong)UIView *seperatorLine;


@end

@implementation CollectedPaperCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//        self.separatorInset = UIEdgeInsetsMake(0, CGFLOAT_MAX, 0, 0);
        
        _iconView = [UIImageView new];
        _titleLabel = [UILabel new];
//        _titleLabel.backgroundColor = [UIColor greenColor];
        
        _infoLabel = [UILabel new];
        _infoLabel.textColor = [UIColor lightGrayColor];
        _infoLabel.font = [UIFont systemFontOfSize:14];
//        _infoLabel.backgroundColor = [UIColor blueColor];
        _infoLabel.textAlignment = NSTextAlignmentRight;
        
        // 分割线
        _seperatorLine = [UIView new];
        _seperatorLine.backgroundColor = [UIColor lightGrayColor];
        
        [self addSubview:_iconView];
        [self addSubview:_titleLabel];
        [self addSubview:_infoLabel];
        [self addSubview:_seperatorLine];

        [self updateUIFrame];
    }
    return self;
}

-(void)updateUIFrame{
    CGFloat h = CGRectGetHeight(self.bounds);
    CGFloat w = CGRectGetWidth(self.bounds);
    
    _iconView.frame = CGRectMake(20, h/2-10, 20, 20);
    _titleLabel.frame = CGRectMake(CGRectGetMaxX(_iconView.frame)+20, 0, w-20-80-20*4, h);
    _infoLabel.frame = CGRectMake(CGRectGetMaxX(_titleLabel.frame)+20, 0, 80, h);
    _seperatorLine.frame = CGRectMake(20, h-1, w-20*2, 1);
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [self updateUIFrame];
}

-(void)configureData: (PaperCollect*)collect{
    _iconView.image = [UIImage imageNamed:@"icon_paper_list"];
    _titleLabel.text = collect.paperName;
    _infoLabel.text = [NSString stringWithFormat:@"共 %ld 道题",collect.questions.count];
}

@end


@interface CollectedPaperListController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)PaperParam *param;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)UITableView *tableView;

@end


@implementation CollectedPaperListController

-(instancetype)initWithPaperParam: (PaperParam*)param{
    if (self = [super init]) {
        _param = param;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self loadDataWithHUDShowing:true];
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[CollectedPaperCell class] forCellReuseIdentifier:@"CollectedPaperCell"];
        
        self.view.autoresizesSubviews = true;
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _tableView.autoresizesSubviews = true;
    }
    return _tableView;
}

-(void)setupUI{
    [self.view addSubview:self.tableView];
    
    self.navigationController.navigationBar.translucent = false;
    
    // nav title
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(dismissAction:)];
}

-(void)dismissAction: (UIBarButtonItem*)item{
    [self dismissViewControllerAnimated:true completion:nil];
}

-(void)setDataSource:(NSMutableArray *)dataSource{
    _dataSource = dataSource;
    [self.tableView reloadData];
    
    self.navigationItem.title = [NSString stringWithFormat:@"共 %ld 套试卷",dataSource.count];
}

-(void)loadDataWithHUDShowing: (BOOL)show {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:true];
    hud.label.text = @"正在加载收藏试卷...";
    if (show) {
        [hud showAnimated:true];
    }
    [[PaperDataManager sharedManager] getCollectedPaperQuestionsNewWithPaperParam:_param succeed:^(NSArray *questions) {
        NSLog(@"收藏列表: %@",questions);
        self.dataSource = [NSMutableArray arrayWithArray:questions];
        [hud hideAnimated: true];
    } failed:^(NSError *error) {
        NSLog(@"%@: ",error.domain);
        hud.label.text = error.domain;
        [hud hideAnimated:true afterDelay:2];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self loadDataWithHUDShowing:false];
}

#pragma mark - Table view data source
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CollectedPaperCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CollectedPaperCell" forIndexPath:indexPath];
    [cell configureData:_dataSource[indexPath.row]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    PaperCollect *collect = _dataSource[indexPath.row];
    NSDictionary *dict = @{
       @"userId": @"1790954",
       @"loginName": @"zxh123",
       @"paperId": collect.paperId,
       @"submited": @"1"
    };
    DoingPaperController *dvc = [[DoingPaperController alloc] initWithParams:dict submitPaperCompletion:nil];
    dvc.isCollectQuestionsScanning = true;
    dvc.collectQuestions = collect.questions;
    [self.navigationController pushViewController:dvc animated:true];
}


@end
